<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>发送一条 MySQL 查询</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.mysql-ping.html">mysql_ping</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.mysql-real-escape-string.html">mysql_real_escape_string</a></div>
 <div class="up"><a href="ref.mysql.html">MySQL 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.mysql-query" class="refentry">
   <div class="refnamediv">
    <h1 class="refname">mysql_query</h1>
    <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">mysql_query</span> &mdash; <span class="dc-title">发送一条 MySQL 查询</span></p>

   </div>
   <div class="refsect1 unknown-unknown-unknown-unknown-unknown-unknown-unknown-unknown-unknown-unknown-unknown-unknown-unknown-unknown-unknown-seealtd" id="refsect1-function.mysql-query-unknown-unknown-unknown-unknown-unknown-unknown-unknown-unknown-unknown-unknown-unknown-unknown-unknown-unknown-unknown-seealtd">
    <h3 class="title">说明</h3>
     <div class="methodsynopsis dc-description">
      <span class="type">resource</span> <span class="methodname"><strong>mysql_query</strong></span>
       ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$query</code></span>
      [, <span class="methodparam"><span class="type">resource</span> <code class="parameter">$link_identifier</code></span>
     ] )</div>

    <p class="para rdfs-comment">
     <span class="function"><strong>mysql_query()</strong></span>
     向与指定的连接标识符关联的服务器中的当前活动数据库发送一条查询。如果没有指定
     <em><code class="parameter">link_identifier</code></em>，则使用上一个打开的连接。如果没有打开的连接，本函数会尝试无参数调用
     <span class="function"><a href="function.mysql-connect.html" class="function">mysql_connect()</a></span>
     函数来建立一个连接并使用之。查询结果会被缓存。
    </p>
    <blockquote class="note"><p><strong class="note">Note</strong>: 
     <p class="para">
      查询字符串不应以分号结束。
     </p>
    </p></blockquote>
    <p class="para">
     <span class="function"><strong>mysql_query()</strong></span> 仅对
     SELECT，SHOW，EXPLAIN 或 DESCRIBE
     语句返回一个资源标识符，如果查询执行不正确则返回
     <strong><code>FALSE</code></strong>。对于其它类型的 SQL
     语句，<span class="function"><strong>mysql_query()</strong></span>
     在执行成功时返回 <strong><code>TRUE</code></strong>，出错时返回 <strong><code>FALSE</code></strong>。非
     <strong><code>FALSE</code></strong> 的返回值意味着查询是合法的并能够被服务器执行。这并不说明任何有关影响到的或返回的行数。
     很有可能一条查询执行成功了但并未影响到或并未返回任何行。
    </p>
    <p class="para">
     以下查询语法上有错，因此
     <span class="function"><strong>mysql_query()</strong></span>
     失败并返回 <strong><code>FALSE</code></strong>：
     <div class="example" id="example-1579">
      <p><strong>Example #1 <span class="function"><strong>mysql_query()</strong></span> 例子</strong></p>
      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
&lt;php<br />$result&nbsp;=&nbsp;mysql_query("SELECT&nbsp;*&nbsp;WHERE&nbsp;1=1")<br />&nbsp;&nbsp;&nbsp;&nbsp;or&nbsp;die("Invalid&nbsp;query:&nbsp;"&nbsp;.&nbsp;mysql_error());<br />?&gt;</span>
</code></div>
      </div>

     </div>
    </p>
    <p class="para">
     以下查询当
     <em>my_col</em>
     并不是表
     <em>my_tbl</em>
     中的列时语义上有错，因此
     <span class="function"><strong>mysql_query()</strong></span>
     失败并返回 <strong><code>FALSE</code></strong>：
     <div class="example" id="example-1580">
      <p><strong>Example #2 <span class="function"><strong>mysql_query()</strong></span></strong></p>
      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">mysql_query</span><span style="color: #007700">(</span><span style="color: #DD0000">"SELECT&nbsp;my_col&nbsp;FROM&nbsp;my_tbl"</span><span style="color: #007700">)<br />&nbsp;&nbsp;&nbsp;&nbsp;or&nbsp;die(</span><span style="color: #DD0000">"Invalid&nbsp;query:&nbsp;"&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">mysql_error</span><span style="color: #007700">());<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
      </div>

     </div>
    </p>
    <p class="para">
     如果没有权限访问查询语句中引用的表时，<span class="function"><strong>mysql_query()</strong></span>
     也会返回 <strong><code>FALSE</code></strong>。
    </p>
    <p class="para">
     假定查询成功，可以调用
     <span class="function"><a href="function.mysql-num-rows.html" class="function">mysql_num_rows()</a></span>
     来查看对应于 SELECT
     语句返回了多少行，或者调用
     <span class="function"><a href="function.mysql-affected-rows.html" class="function">mysql_affected_rows()</a></span>
     来查看对应于
     DELETE，INSERT，REPLACE 或 UPDATE
     语句影响到了多少行。
    </p>
    <p class="para">
     仅对 SELECT，SHOW，DESCRIBE 或 EXPLAIN 语句
     <span class="function"><strong>mysql_query()</strong></span>
     才会返回一个新的结果标识符，可以将其传递给
     <span class="function"><a href="function.mysql-fetch-array.html" class="function">mysql_fetch_array()</a></span>
     和其它处理结果表的函数。处理完结果集后可以通过调用
     <span class="function"><a href="function.mysql-free-result.html" class="function">mysql_free_result()</a></span>
     来释放与之关联的资源，尽管脚本执行完毕后会自动释放内存。
    </p>
    <p class="para">
     参见
     <span class="function"><a href="function.mysql-num-rows.html" class="function">mysql_num_rows()</a></span>，<span class="function"><a href="function.mysql-affected-rows.html" class="function">mysql_affected_rows()</a></span>，<span class="function"><a href="function.mysql-unbuffered-query.html" class="function">mysql_unbuffered_query()</a></span>，<span class="function"><a href="function.mysql-free-result.html" class="function">mysql_free_result()</a></span>，<span class="function"><a href="function.mysql-fetch-array.html" class="function">mysql_fetch_array()</a></span>，<span class="function"><a href="function.mysql-fetch-row.html" class="function">mysql_fetch_row()</a></span>，<span class="function"><a href="function.mysql-fetch-assoc.html" class="function">mysql_fetch_assoc()</a></span>，<span class="function"><a href="function.mysql-result.html" class="function">mysql_result()</a></span>，<span class="function"><a href="function.mysql-select-db.html" class="function">mysql_select_db()</a></span> 和
     <span class="function"><a href="function.mysql-connect.html" class="function">mysql_connect()</a></span>。
    </p>
   </div>

  </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.mysql-ping.html">mysql_ping</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.mysql-real-escape-string.html">mysql_real_escape_string</a></div>
 <div class="up"><a href="ref.mysql.html">MySQL 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
